<template>
  <div>
    <!-- <h3>商品列表</h3> -->
    <!-- 面包屑导航 ============================-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域================================== -->
    <el-card>
      <!-- 第一行   -->
      <el-row :gutter="20">
        <!-- 第一列==================================================  -->
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="querData.query"
            clearable
            ref="searchInput"
            @clear="getGoodsList"
          >
            <el-button
              @click="search"
              slot="append"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 第二列 ==================================================-->
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品table表格区域=========================================================== -->
      <el-table :data="goodsList" border stripe style="width: 100%">
        <!-- 索引列 -->
        <el-table-column type="index" label="#"> </el-table-column>

        <!-- 数据列 -->
        <el-table-column prop="goods_name" label="商品名称" width="600">
        </el-table-column>

        <el-table-column prop="goods_price" label="价格" width="60">
        </el-table-column>

        <el-table-column prop="goods_weight" label="数量" width="60">
        </el-table-column>

        <!-- 添加时间过滤器 -->
        <el-table-column prop="add_time" label="添加时间 " width="150">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>

        <!-- 操作列 -->
        <el-table-column label="操作" width="125px">
          <template slot-scope="scope">

            <!-- 修改按钮=============== -->
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
            >

            </el-button>

            <!-- 删除按钮=============== -->

            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="doDel(scope.row)"
            >
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 页码条 ======================================================-->
      <el-pagination
        @size-change="handleSizeChange"
        :current-page="querData.pagenum"
        :page-size="querData.pagesize"
        :page-sizes="[5, 10, 20, 50]"
        @current-change="pageChange"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>


<script>
export default {
  data() {
    return {
      //分页请求参数对象
      querData: {
        query: '', //查询关键字
        pagenum: 1, //当前页码
        pagesize: 5, //页容量(每页显示的行数)
      },
      //商品列表
      goodsList: [],
      //总商品数量
      total: 0,
    }
  },

  // 钩子函数 相当于vue入口函数 当配置对象成员 并挂载到实例后执行
  //created==========================================================================
  created() {
    //调用方法 请求并渲染商品列表
    this.getGoodsList()
  },

  // methods  ========================================================================
  methods: {
    //根据分页获取对应的商品列表
    async getGoodsList() {
      const { data: resData } = await this.$http.get(`goods`, {
        params: this.querData,
      })
      //如果请求失败
      if (resData.meta.status !== 200) {
        return this.$message.error(resData.meta.msg)
      }
      //如果请求成功将数组设置给data.goodsList数组
      this.$message.success(resData.meta.msg)
      this.goodsList = resData.data.goods
      this.total = resData.data.total
      console.log(resData)
    },
    //页容量改变事件
    handleSizeChange(newSize) {
      //a修改查询参数对象的页容量
      this.querData.pagesize = newSize
      //b重新请求商品列表
      this.getGoodsList()
    },
    //页码改变事件
    pageChange(newPage) {
      //修改查询参数对象的当前页码
      this.querData.pagenum = newPage
      //重新请求商品列表
      this.getGoodsList()
    },
    //搜索事件
    search() {
      //判断关键字是否为空
      if (this.querData.query.trim().length == 0) {
        //让文本框获取焦点
        this.$refs.searchInput.focus()
        //停止代码执行 并提示消息
        return this.$message.warning('请输入搜索关键字')
      }
      //请求商品列表
      this.getGoodsList()
    },
    //删除按钮事件
    doDel(rowData) {
      this.$MessageBox.confirm('确定删除吗?', '系统提示', {
        confirmButtonText: '确定真的删除吗?',
        callback: async (action) => {
            //如果取消 则停止代码执行
          if (action == 'cancel') return
          //如果确定 则请求删除接口
          const delId = rowData.goods_id
          const { data: resData } = await this.$http.delete(`goods/${delId}`)
          //如果请求失败
          if (resData.meta.status !== 200)
            return this.$message.error('resData.meta.msg')
            //如果请求成功  更新列表
          this.getGoodsList()
        },
      })
    },
    //跳转到添加商品页面
    goAddpage() {
        this.$router.push('/goods/add')   //修改1.html #/goods/add  改锚点
          //  window.location.href = '/goods/add'  改完整的地址 
    }
  },
}
</script>



<style lang="less" scoped>
</style>